import { Popconfirm, Input, List, Space, message } from "antd";
import { useModel } from "umi";
import { useReactive } from "ahooks";

import useDeveloper from "../../hooks/resource/useDeveloper";

const AddDeveloperModal = ({ developers }) => {
    const { remoteData } = useModel("global");
    const { onCloseGlobalModal } = useModel("global.modal");
    const { loginInfo } = useModel("person.info");
    const { submitLoading, addDeveloper } = useDeveloper();

    const state = useReactive({
        organizations: [],
        loading: false,
    });

    const handleSearch = async (value) => {
        if (value) {
            state.loading = true;
            let res = await remoteData({
                object: "public-user-auth-center",
                serviceUrl: loginInfo.authCenterUrl,
                serviceName: "/api/v1/organization/searchFullName",
                params: { fullName: value },
            });
            let organizations = [];
            res.map((item) =>
                developers?.findIndex((_) => item.uniformSocialCreditCode === _.oiUscc) !== -1 ? organizations.push({ ...item, ishas: true }) : organizations.push({ ...item, ishas: false })
            );
            state.organizations = organizations;
            state.loading = false;
        } else {
            message.warn("名称不能为空");
        }
    };

    return (
        <Space direction="vertical" size={10} style={{ width: "100%" }}>
            <Input.Search placeholder="搜索组织名称" onSearch={handleSearch} allowClear enterButton />
            <List
                loading={state.loading}
                header={"组织列表"}
                bordered
                size="small"
                dataSource={state.organizations}
                renderItem={(item) => (
                    <List.Item
                        actions={[
                            item.ishas ? (
                                <div>已添加</div>
                            ) : (
                                <Popconfirm
                                    title="确定要添加当前组织为开发组织吗?"
                                    placement="topRight"
                                    okButtonProps={{ loading: submitLoading }}
                                    onConfirm={() => {
                                        addDeveloper(item.uniformSocialCreditCode, (res) => {
                                            if (res) {
                                                message.success("成功添加开发商");
                                                onCloseGlobalModal();
                                            }
                                        });
                                    }}
                                >
                                    <a>添加</a>
                                </Popconfirm>
                            ),
                        ]}
                    >
                        {item.fullName}
                    </List.Item>
                )}
            />
        </Space>
    );
};

export default AddDeveloperModal;
